package com.eastcom.bm.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.eastcom.bm.atom.Atom_CalendarModel;
import com.eastcom.bm.atom.CalendarPolicy;
import com.eastcom.bm.dao.AbstractDAO;
import com.eastcom.bm.dao.ICalendarPolicyDao;
import com.eastcom.common.db.JDBCTemplate;
import com.eastcom.common.db.ResultData;

public class CalendarPolicyDao extends AbstractDAO implements ICalendarPolicyDao {

	public List<CalendarPolicy> getAllCalendarPolicy() throws Exception {
		// TODO Auto-generated method stub
		try{
			List<CalendarPolicy> results=new ArrayList<CalendarPolicy>();

		    String sql="select p.UUID as POLICYUUID,p.NAME as POLICYNAME,p.REMARK,m.*,p.PRIORITY from QA_CALENDARPOLICY p,QA_CALENDARMODEL m" +
		    		" where p.CALENDAR_MODEL_UUID=m.UUID(+)";
			ResultData rd=JDBCTemplate.executeSQLQuery(sql, JDBCTemplate.PROPAGATION_NEVER, false, false);
			while(rd.next()){
				CalendarPolicy calendarPolicy=new CalendarPolicy();	//此处采用的是手动ORM,可采用AbstractDao中的利用反射进行ORM

				calendarPolicy.setUuid(rd.getString("POLICYUUID"));
				calendarPolicy.setName(rd.getString("POLICYNAME"));
				calendarPolicy.setRemark(rd.getString("REMARK"));
				calendarPolicy.setPriority(rd.getString("PRIORITY"));
				if(rd.getString("UUID")!=null){
					Atom_CalendarModel calendarModel=new Atom_CalendarModel();
					calendarPolicy.setCalendarModel(calendarModel);
					
					calendarModel.setUuid(rd.getString("UUID"));
					calendarModel.setName(rd.getString("NAME"));
					calendarModel.setHour0("0".equals(rd.getString(6))?false:true);
					calendarModel.setHour1("0".equals(rd.getString(7))?false:true);
					calendarModel.setHour2("0".equals(rd.getString(8))?false:true);
					calendarModel.setHour3("0".equals(rd.getString(9))?false:true);
					calendarModel.setHour4("0".equals(rd.getString(10))?false:true);
					calendarModel.setHour5("0".equals(rd.getString(11))?false:true);
					calendarModel.setHour6("0".equals(rd.getString(12))?false:true);
					calendarModel.setHour7("0".equals(rd.getString(13))?false:true);
					calendarModel.setHour8("0".equals(rd.getString(14))?false:true);
					calendarModel.setHour9("0".equals(rd.getString(15))?false:true);
					calendarModel.setHour10("0".equals(rd.getString(16))?false:true);
					calendarModel.setHour11("0".equals(rd.getString(17))?false:true);
					calendarModel.setHour12("0".equals(rd.getString(18))?false:true);
					calendarModel.setHour13("0".equals(rd.getString(19))?false:true);
					calendarModel.setHour14("0".equals(rd.getString(20))?false:true);
					calendarModel.setHour15("0".equals(rd.getString(21))?false:true);
					calendarModel.setHour16("0".equals(rd.getString(22))?false:true);
					calendarModel.setHour17("0".equals(rd.getString(23))?false:true);
					calendarModel.setHour18("0".equals(rd.getString(24))?false:true);
					calendarModel.setHour19("0".equals(rd.getString(25))?false:true);
					calendarModel.setHour20("0".equals(rd.getString(26))?false:true);
					calendarModel.setHour21("0".equals(rd.getString(27))?false:true);
					calendarModel.setHour22("0".equals(rd.getString(28))?false:true);
					calendarModel.setHour23("0".equals(rd.getString(29))?false:true);
					calendarModel.setSUN("0".equals(rd.getString(30))?false:true);
					calendarModel.setMON("0".equals(rd.getString(31))?false:true);
					calendarModel.setTUE("0".equals(rd.getString(32))?false:true);
					calendarModel.setWEN("0".equals(rd.getString(33))?false:true);
					calendarModel.setTHU("0".equals(rd.getString(34))?false:true);
					calendarModel.setFRI("0".equals(rd.getString(35))?false:true);
					calendarModel.setSAT("0".equals(rd.getString(36))?false:true);
				}
				
				results.add(calendarPolicy);
			}

			return results;
		}
		catch(Exception e){
			throw e;
		}

	}

	public CalendarPolicy getCalendarPolicyByUUID(String policyUUID) throws Exception {
		// TODO Auto-generated method stub
		try{
			String sql="select p.UUID as POLICYUUID,p.NAME as POLICYNAME,p.REMARK,m.* from CALENDARPOLICY p,CALENDATMODEL m" +
		    		" where p.UUID='"+policyUUID+"' and p.CALENDAR_MODEL_UUID=m.UUID(+)";
			ResultData rd=JDBCTemplate.executeSQLQuery(sql, JDBCTemplate.PROPAGATION_NEVER, false, false);
			if(rd.next()){
				CalendarPolicy result=new CalendarPolicy();

				result.setUuid(rd.getString(1));
				result.setName(rd.getString(2));
				result.setRemark(rd.getString(3));
				
				if(rd.getString(4)!=null){
					Atom_CalendarModel calendarModel=new Atom_CalendarModel();
					result.setCalendarModel(calendarModel);
					
					calendarModel.setUuid(rd.getString(4));
					calendarModel.setName(rd.getString(5));
					calendarModel.setHour0("0".equals(rd.getString(6))?false:true);
					calendarModel.setHour1("0".equals(rd.getString(7))?false:true);
					calendarModel.setHour2("0".equals(rd.getString(8))?false:true);
					calendarModel.setHour3("0".equals(rd.getString(9))?false:true);
					calendarModel.setHour4("0".equals(rd.getString(10))?false:true);
					calendarModel.setHour5("0".equals(rd.getString(11))?false:true);
					calendarModel.setHour6("0".equals(rd.getString(12))?false:true);
					calendarModel.setHour7("0".equals(rd.getString(13))?false:true);
					calendarModel.setHour8("0".equals(rd.getString(14))?false:true);
					calendarModel.setHour9("0".equals(rd.getString(15))?false:true);
					calendarModel.setHour10("0".equals(rd.getString(16))?false:true);
					calendarModel.setHour11("0".equals(rd.getString(17))?false:true);
					calendarModel.setHour12("0".equals(rd.getString(18))?false:true);
					calendarModel.setHour13("0".equals(rd.getString(19))?false:true);
					calendarModel.setHour14("0".equals(rd.getString(20))?false:true);
					calendarModel.setHour15("0".equals(rd.getString(21))?false:true);
					calendarModel.setHour16("0".equals(rd.getString(22))?false:true);
					calendarModel.setHour17("0".equals(rd.getString(23))?false:true);
					calendarModel.setHour18("0".equals(rd.getString(24))?false:true);
					calendarModel.setHour19("0".equals(rd.getString(25))?false:true);
					calendarModel.setHour20("0".equals(rd.getString(26))?false:true);
					calendarModel.setHour21("0".equals(rd.getString(27))?false:true);
					calendarModel.setHour22("0".equals(rd.getString(28))?false:true);
					calendarModel.setHour23("0".equals(rd.getString(29))?false:true);
					calendarModel.setSUN("0".equals(rd.getString(30))?false:true);
					calendarModel.setMON("0".equals(rd.getString(31))?false:true);
					calendarModel.setTUE("0".equals(rd.getString(32))?false:true);
					calendarModel.setWEN("0".equals(rd.getString(33))?false:true);
					calendarModel.setTHU("0".equals(rd.getString(34))?false:true);
					calendarModel.setFRI("0".equals(rd.getString(35))?false:true);
					calendarModel.setSAT("0".equals(rd.getString(36))?false:true);
				}
				
				return result;
			}
			
			return null;

		}
		catch(Exception e){
			throw e;
		}
	}

}
